//
//  WmcUserInfoDBManager.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/3/31.
//

import Foundation
import YPBedrock

// MARK: - 扩展WmcUserInfoDBManager + WmcDBProtocol

extension WmcUserInfoDBManager: WmcDBProtocol {
    static var tableName: String = "user_info"

    static var dataType: [WmcDBManager.DataType] {
        return [
            .primaryKey(),
            .varchar(name: "yupao_userInfo"),
            .varchar(name: "wmc_userInfo"),
        ]
    }
}

// MARK: - [WmcUserInfoDBManager]

struct WmcUserInfoDBManager {
    // Internal

    /// 保存鱼泡和水印相机用户信息
    /// - Parameters:
    ///   - yupaoUserInfo: 鱼泡账号信息
    ///   - wmcUserInfo: 水印相机账号信息
    ///   - resultBlock: 保存完成
    static func save(yupaoUserInfo: [String: Any],
                     wmcUserInfo: [String: Any]? = nil,
                     resultBlock: YPBlock.OneParam<Bool>? = nil)
    {
        guard let yupaoUserInfoStr = WmcUserInfoDBManager.dictionaryConvertToJsonString(yupaoUserInfo) else {
            resultBlock?(false)
            return
        }

        var dataList: [WmcDBManager.InsertData] = [
            .init(column: Self.dataType[1], value: yupaoUserInfoStr),
        ]

        if let wmcUserInfo = wmcUserInfo {
            guard let wmcUserInfoStr = WmcUserInfoDBManager.dictionaryConvertToJsonString(wmcUserInfo) else {
                resultBlock?(false)
                return
            }
            dataList.append(.init(column: Self.dataType[2], value: wmcUserInfoStr))
        }

        WmcDBManager.shared.insertData(tableName: Self.tableName, insertData: dataList, resultBlock: resultBlock)
    }

    /// 保存鱼泡账号信息
    /// - Parameters:
    ///   - yupaoUserInfo: 账号信息
    ///   - resultBlock: 结果
    static func save(yupaoUserInfo: [String: Any],
                     resultBlock: YPBlock.OneParam<Bool>? = nil)
    {
        guard let yupaoUserInfoStr = WmcUserInfoDBManager.dictionaryConvertToJsonString(yupaoUserInfo) else {
            resultBlock?(false)
            return
        }

        let dataList: [WmcDBManager.InsertData] = [
            .init(column: Self.dataType[1], value: yupaoUserInfoStr),
        ]

        WmcDBManager.shared.insertData(tableName: Self.tableName, insertData: dataList, resultBlock: resultBlock)
    }

    /// 查询登录信息
    /// - Parameter resultBlock: 结果
    static func query(resultBlock: YPBlock.OneParam<[String: Any]?>? = nil) {
        WmcDBManager.shared.searchCustomData(tableName: Self.tableName) { list in
            if list.count > 0 {
                resultBlock?(list.first)
            } else {
                resultBlock?(nil)
            }
        }
    }

    /// 更新登录缓存信息
    /// - Parameters:
    ///   - wmcUserInfo: 水印相机账号信息
    ///   - yupaoUserInfo: 账号信息
    ///   - resultBlock: 结果
    static func update(wmcUserInfo: [String: Any]? = nil,
                       yupaoUserInfo: [String: Any]? = nil,
                       resultBlock: YPBlock.OneParam<Bool>? = nil)
    {
        var dataList: [WmcDBManager.InsertData] = []

        if let wmcUserInfo = wmcUserInfo, let wmcUserInfoStr = WmcUserInfoDBManager.dictionaryConvertToJsonString(wmcUserInfo) {
            dataList.append(.init(column: Self.dataType[2], value: wmcUserInfoStr))
        }

        if let yupaoUserInfo = yupaoUserInfo, let yupaoUserInfoStr = WmcUserInfoDBManager.dictionaryConvertToJsonString(yupaoUserInfo) {
            dataList.append(.init(column: Self.dataType[1], value: yupaoUserInfoStr))
        }

        guard !dataList.isEmpty else {
            resultBlock?(false)
            return
        }
        WmcDBManager.shared.updateData(tableName: Self.tableName, updateData: dataList, resultBlock: resultBlock)
    }

    /// 清除登录信息
    /// - Parameter resultBlock: 结果
    static func delete(resultBlock: YPBlock.OneParam<Bool>? = nil) {
        WmcDBManager.shared.deleteCustomData(tableName: Self.tableName, resultBlock: resultBlock)
    }

    // Private

    /// <#Description#>
    /// - Parameter params: <#params description#>
    /// - Returns: <#description#>
    private static func dictionaryConvertToJsonString(_ params: [String: Any]) -> String? {
        let data = try? JSONSerialization.data(withJSONObject: params, options: [])
        let str = String(data: data!, encoding: String.Encoding.utf8)
        return str
    }
}
